package borge.web.teamspirit.client.page.shared.widgets;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.servercomm.ServerComm;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginWidget extends BaseWidget implements ClickHandler{

	private TextBox iUsername;
	private PasswordTextBox iPassword;
	private Button iLogin;
	
	//private HorizontalPanel iHorizontal;
	private VerticalPanel iVertical;
	
	public final int LAYOUT_AS_ROW = 1;
	public final int LAYOUT_AS_COLUMN = 2;
	public final int LAYOUT_COLUMN_AND_ROW = 3;
	
	//private final DataServiceAsync dataService = GWT.create(DataService.class);
	
	public LoginWidget(WidgetEventHandler weh, String aPixelWidth){
		
		super(weh);
		
		iUsername = new TextBox();
		iPassword = new PasswordTextBox();
		iLogin = new Button("Login");
		iLogin.addClickHandler(this);
		
		reset();
		iUsername.setWidth(aPixelWidth);
		iPassword.setWidth(aPixelWidth);
		
		iUsername.addFocusHandler(new FocusHandler(){
			public void onFocus(FocusEvent aEvent){
				if(iUsername.getText().equals(TeamSpirit.getConstants().username()))
					iUsername.setText("");
			}
		});
		
		iPassword.addFocusHandler(new FocusHandler(){
			public void onFocus(FocusEvent aEvent){
				if(iPassword.getText().equals(TeamSpirit.getConstants().password()))
					iPassword.setText("");
			}
		});
		
		iVertical = new VerticalPanel();
		iVertical.add(iUsername);
		iVertical.add(iPassword);
		iVertical.add(iLogin);
		
		initWidget(iVertical);
		
	}
	
	public void reset(){
		iUsername.setText(TeamSpirit.getConstants().username());
		iPassword.setText(TeamSpirit.getConstants().password());
	}
	
	public String getUsername(){
		return iUsername.getText();
	}
	
	public String getPassword(){
		return iPassword.getText();
	}
	

	
	public void onClick(ClickEvent aEvent) {
		
		//ServerComm.loginUser(getUsername, aPassword, aCallbackHandler)
		postEvent(WidgetEventType.LOGGED_IN);
		
		//ServerComm.getObjectList(null, null);
		//Window.alert("Username = " + username + " - password = " + password);

		/*
		dataService.greetServer("andreas",
				new AsyncCallback<Match>() {
					public void onFailure(Throwable caught) {
						Window.alert("failed");
					}

					public void onSuccess(Match result) {
						//Window.alert(result);
						String str = "Id = " + result.getId() + ": periods = " + result.getPeriods();
						Window.alert(str);
					}
				});*/
		// check with database
		
	}

}
